import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 存放购物车商品
    arr:[ 
      {kind:'手机', price:1000, count:1 },
      {kind:'电脑', price:2000, count:1 },
      {kind:'键盘', price:200, count:1 },
    ]
  },
  mutations: {
    // 商品数量加1
    add(state, id) {
      state.arr[id].count++;
    },
    // 商品数量减1
    des(state, id) {
      if (state.arr[id].count > 1) {
        state.arr[id].count--;
      }
    },
  },
  actions: {
  },
  getters: {
    // 计算购物车总价
    getSum(state){
      return state.arr.reduce((sum, item) => sum + item.count * item.price, 0)
    }
  },
  modules: {
  }
})
